package com.erp.fin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.erp.common.utils.*;
import com.erp.fin.domain.BillingDO;
import com.erp.fin.domain.PaymentDO;
import com.erp.fin.domain.ReceiptTickDO;
import com.erp.fin.domain.ReimburseTypeDO;
import com.erp.fin.service.ReceiptTickService;
import com.erp.fin.service.ReimburseTypeService;
import com.erp.pro.domain.SupplierDO;
import com.erp.pro.service.SupplierService;
import com.erp.pur.domain.GroupDO;
import com.erp.pur.service.GroupService;
import com.erp.system.dao.UserDao;
import com.erp.system.domain.UserDO;
import com.erp.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-10-24 15:53:38
 */
 
@Controller
@RequestMapping("/fin/receiptTick")
public class ReceiptTickController {
	@Autowired
	private ReceiptTickService receiptTickService;

	@Autowired
	private GroupService groupService;

	@Autowired
	private SupplierService supplierService;

	@Autowired
	private UserService userService;
	@Autowired
	private ReimburseTypeService reimburseTypeService;
	
	@GetMapping()
	String ReceiptTick(){
	    return "fin/receiptTick/receiptTick";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ReceiptTickDO> receiptTickList = receiptTickService.list(query);
		int total = receiptTickService.count(query);
		PageUtils pageUtils = new PageUtils(receiptTickList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(Model model){
	    return "fin/receiptTick/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		ReceiptTickDO receiptTick = receiptTickService.get(id);
		model.addAttribute("receiptTick", receiptTick);
		List<GroupDO> groupDOList = groupService.list(null);
		model.addAttribute("groupDOList", groupDOList);
		List<SupplierDO> supplierDOList = supplierService.list(null);
		model.addAttribute("supplierDOList", supplierDOList);
//		List<UserDO> userD0List = userService.list(null);
		List<UserDO> userD0List = userService.getlist(null);
		model.addAttribute("userD0List", userD0List);

		List<ReimburseTypeDO> reimburseTypeList = reimburseTypeService.list(null);
		model.addAttribute("reimburseTypeList", reimburseTypeList);
		return "fin/receiptTick/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( ReceiptTickDO receiptTick){
		if(receiptTick.getPurId() != null){
			GroupDO groupDO = groupService.get(receiptTick.getPurId());
			receiptTick.setPurGroup(groupDO.getGroupid());
		}
		if(receiptTick.getSupplierId() != null){
			SupplierDO supplierDO = supplierService.get(receiptTick.getSupplierId());
			receiptTick.setSupplierName(supplierDO.getName());
		}

		if(receiptTick.getInvoiceUserId() != null){
			UserDO userDO = userService.get(receiptTick.getInvoiceUserId());
			receiptTick.setInvoiceUserName(userDO.getName());
		}
		receiptTick.setCreateTime(new Date());
		receiptTick.setFlag(1);
		if(receiptTickService.save(receiptTick)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( ReceiptTickDO receiptTick){
		receiptTickService.update(receiptTick);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(receiptTickService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}

	@PostMapping( "/confirm")
	@ResponseBody
	public R confirm( Long id){
		if(receiptTickService.confirm(id)>0){
			//更新收票金额
			ReceiptTickDO receiptTickDO = receiptTickService.get(id);
			groupService.updateinvoicePrice(receiptTickDO.getPurId(),receiptTickDO.getTickAmount());
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids){
		receiptTickService.batchRemove(ids);
		return R.ok();
	}


	@PostMapping("/exportData")
	@ResponseBody
	public void downloadTemplate(ReceiptTickDO receiptTick, HttpServletResponse response){
		//ProjectDO project
		Map<String, Object> params = new HashMap<>();
		if(StringUtils.isNotBlank(receiptTick.getPurGroup())){
			params.put("purGroup",receiptTick.getPurGroup());
		}
		if(receiptTick.getTaxRate() != null){
			params.put("taxRate",receiptTick.getTaxRate());
		}
		if(StringUtils.isNotBlank(receiptTick.getInvoiceUserName())){
			params.put("invoiceUserName",receiptTick.getInvoiceUserName());
		}
		if(StringUtils.isNotBlank(receiptTick.getSupplierName())){
			params.put("supplierName",receiptTick.getSupplierName());
		}
		if(receiptTick.getInvoiceTimeend() != null){
			params.put("invoiceTimeend",receiptTick.getInvoiceTimeend());
		}
		if(receiptTick.getInvoiceTimeStr() != null){
			params.put("invoiceTimeStr",receiptTick.getInvoiceTimeStr());
		}
		List<ReceiptTickDO> receiptTickDOList = receiptTickService.list(params);
		ExcelUtil.exportExcel(receiptTickDOList,"收票记录","收票记录",ReceiptTickDO.class,"收票记录.xls",response);
	}

	@GetMapping("/getInvoiceByitm/{id}")
	String getInvoiceByitm(@PathVariable("id") Long id,Model model){
		model.addAttribute("itmid", id);
		return "fin/receiptTick/receiptTickByitm";
	}

	@ResponseBody
	@GetMapping("/getInvoiceByitmlist")
	public PageUtils getInvoiceByitmlist(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<ReceiptTickDO> receiptTickList = receiptTickService.list(query);
		int total = receiptTickService.count(query);
		PageUtils pageUtils = new PageUtils(receiptTickList, total);
		return pageUtils;
	}

}
